import random


def generate_random_word(min_length=20, max_length=30):
    word = ""
    target_length = random.randint(min_length, max_length)

    for i in range(0, target_length):
        word += chr(random.randint(97, 122))

    return word


def generate_random_nodes(root_name, children_count):
    nodes = [
        {
            "id": 0,
            "label": root_name,
            "group": "0",
            "type": "ROOT",
            "properties": [
                {
                    "name": "名字",
                    "text": "芒果"
                },
                {
                    "name": "用量",
                    "text": "100个咕咕咕咕咕咕过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过"
                }
            ],
            "neighbor1": ["aaa1", "bbb1", "ccc1"],
            "neighbor2": ["aaa2", "bbb2", "ccc2"]
        }
    ]

    for i in range(0, children_count):
        nodes.append({
            "id": i + 1,
            "label": generate_random_word(),
            "group": "1" if random.random() > 0.5 else "2",
            "properties": [],
            "neighbor1": [],
            "neighbor2": []
        })

    return nodes


def generate_random_links(children_count, node_max_son_count=5):
    remaining_children_count = children_count
    current_child_index = 1

    current_father_indexes = [0]

    is_at_root = True

    links = []

    # add children iteratively, level by level
    while remaining_children_count > 0:
        next_father_indexes = []

        for i in current_father_indexes:
            if remaining_children_count <= 0:
                break

            current_node_target_children_count = random.randint(1, node_max_son_count)
            if is_at_root:
                current_node_target_children_count = node_max_son_count
                is_at_root = False

            current_node_target_children_count = \
                min(current_node_target_children_count, remaining_children_count)

            for j in range(current_child_index,
                           current_child_index + current_node_target_children_count):
                label = generate_random_word()

                links.append({
                    "from": i,
                    "to": j,
                    "label": label
                })

                next_father_indexes.append(j)

            remaining_children_count -= current_node_target_children_count
            current_child_index += current_node_target_children_count

        current_father_indexes = next_father_indexes

    # extra link added

    links.append({
        "from": children_count - 1,
        "to": children_count - 2,
        "label": "LAST"
    })

    links.append({
        "from": children_count - 1,
        "to": children_count - 3,
        "label": "LAST"
    })

    links.append({
        "from": children_count - 1,
        "to": children_count - 4,
        "label": "LAST"
    })

    links.append({
        "from": children_count - 1,
        "to": children_count - 5,
        "label": "LAST"
    })

    return links


def generate_random_graph(root_name, min_children_count=5, max_children_count=35):
    children_count = random.randint(min_children_count, max_children_count)
    data = {
        "nodes": generate_random_nodes(root_name, children_count),
        "edges": generate_random_links(children_count)
    }

    return data


def generate_random_route(orig_name, dest_name, min_count=80, max_count=100):
    children_count = random.randint(min_count, max_count)
    nodes = []
    edges = []

    nodes.append({
        "id": 0,
        "label": orig_name,
        "group": "0",
        "type": "ROOT",
        "properties": [
            {
                "name": "名字",
                "text": "芒果"
            },
            {
                "name": "用量",
                "text": "100个咕咕咕咕咕咕过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过过"
            }
        ],
        "neighbor1": ["aaa1", "bbb1", "ccc1"],
        "neighbor2": ["aaa2", "bbb2", "ccc2"]
    })

    for i in range(1, children_count + 1):
        nodes.append({
            "id": i,
            "group": "1",
            "label": generate_random_word(),
            "properties": [],
            "neighbor1": [],
            "neighbor2": []
        })

        edges.append({
            "from": i - 1,
            "to": i,
            "label": generate_random_word()
        })

    nodes.append({
        "id": children_count + 1,
        "group": "2",
        "label": dest_name,
        "properties": [],
        "neighbor1": [],
        "neighbor2": []
    })

    edges.append({
        "from": children_count,
        "to": children_count + 1,
        "label": generate_random_word()
    })

    data = {
        "nodes": nodes,
        "edges": edges
    }

    return data
